// document.oncontextmenu=function(){return false;}

/** ***************************************************** */
var SVR_CTX="";
var SVR_CTX_PATH = ""+SVR_CTX;
var S_SVR_CTX_PATH = "http://s.waigo.com"+SVR_CTX;

GET_QS_PARAM=function(name)
{
	var href = window.location.href;
	var p1 = href.indexOf("?"+name+"=");
	if(p1==-1) p1 = href.indexOf("&"+name+"=");
	if(p1==-1) return "";
	var p2 = href.indexOf("&",p1+1);
	if(p2==-1) p2 = href.length;
	return href.substring(p1+name.length+2,p2);
};
/**
 * 对URL进行附加随机子，以避免缓存
 * @param {} url
 * @return {}
 */
URL_FMT=function(url)
{
    var aparam = "rndxxx=";
    var d = new Date();
    var seed = d.getMilliseconds()+""+d.getTime();
    var idx = url.lastIndexOf(aparam);
    if(idx==-1)
    {
        url += (url.indexOf("?")>-1 ? "&" : "?") + aparam + seed;
    }else{
        url = url.substring(0,idx+ aparam.length) + seed;
    }
    return url;
};
    
__PRI_LOADING=function(t)
{
    var loading = $("#_GBL_LOADING");
    if(t==1)
    {
        var w = $(window).width();
        var h = $(window).height();
        
        loading.addClass("PRI_LOADING");
        loading.css({"left":((w/2-100)+"px"),"top":((h/2-15)+"px")});
        loading.text("正在处理中，请稍后...");
        loading.show();
    }else{
        loading.hide();
    }
};

GBL_JSON_REQ_T=function(method,enableTip,reqUrl,reqData,callbackHandle,timeoutHandle)
{
    $.ajax({
        type: method,
        url: reqUrl,
        data: reqData,
        dataType: "text",
        beforeSend:function(){if(enableTip) __PRI_LOADING(1);},
        complete:function(){if(enableTip) __PRI_LOADING(0);},
        success: function(ret, textStatus){
            if (textStatus == "success") {
                var jo = eval("("+ret+")");
                if(jo.status=="timeout" || jo.status=="failure")
                {
                    if(timeoutHandle)
                    {
                        timeoutHandle.call(this, jo);
                    }else
                        alert(jo.values);
                }else{
                    callbackHandle.call(this, jo);
                }
            }else{
                alert("很遗憾，您发起的请求通信失败");
            }
        }
    });
};

/**
 * 想服务端发送POST的AJAX请求
 * @param {} reqUrl
 * @param {} reqData
 * @param {} callbackHandle
 */
GBL_JSON_POST=function(reqUrl,reqData,callbackHandle,timeoutHandle)
{
	GBL_JSON_REQ_T('POST',true,reqUrl,reqData,callbackHandle,timeoutHandle);
};

/**
 * 想服务端发送GET的AJAX请求
 * @param {} reqUrl
 * @param {} reqData
 * @param {} callbackHandle
 */
GBL_JSON_GET=function(reqUrl,reqData,callbackHandle,timeoutHandle)
{
	GBL_JSON_REQ_T('GET',true,reqUrl,reqData,callbackHandle,timeoutHandle);
};

/**
 * 判断对象是否存在
 */
IS_PGOBJ=function(target,o)
{
	if(o)
		return target.length>0;
	else
		return $(target).length>0;
};
/**
 * 判断是否为空
 * @param {} str
 * @return {}
 */
isEmpty=function(str)
{
	return (str==null || str==undefined || $.trim(str).length==0);
};
toStr=function(str)
{
	return (str==null || str==undefined) ? "" : str;
};
/**
 * 格式化日期
 */
FMT_DATE=function(date){
	var y = date.getFullYear();
	var m = date.getMonth()+1;
	if(m<10) m="0"+m;
	var d = date.getDate();
	if(d<10) d="0"+d;
	return y+'-'+m+'-'+d;
};

/**
 * 获取当前时间
 */
GBL_getCurDate=function()
{
	var today = new Date();
	return FMT_DATE(today);
};

/**
* @serializedParams looks like "prop1=value1&prop2=value2"
* Nested property like 'prop.subprop=value' is also supported 
*/	
paramString2obj=function(formFields)
{
 	var obj={};
    function evalThem (field)
	{
		var attributeName = field.name;
		var attributeValue = field.value;
		if(!attributeValue) return ;
         
		var array = attributeName.split(".");
		for (var i = 1; i < array.length; i++)
		{
            var tmpArray = Array();
            tmpArray.push("obj");
            for (var j = 0; j < i; j++)
			{
                tmpArray.push(array[j]);
            };
            var evalString = tmpArray.join(".");
            if(!eval(evalString))
			{
                eval(evalString+"={};");               
            }
        };
        obj[attributeName] = $.trim(attributeValue);
    };
    
    $.each(formFields,function(i,field){
		evalThem(field);
    });
    return obj;
};
/**
 * 扩展功能，将form的值转换成json序列
 */
$.fn.form2json = function()
{
	var obj = paramString2obj(this.serializeArray());
	return obj;
};


/**
 * 对指定ID的下面的省市区数据进行联动数据绑定
 */
_regRangeSelectHandle=function(otgo,fids)
{
	if(!fids) fids=["province", "city", "district"];
	
    $("select",otgo).each(function(){
        var field = $(this);
        field.change(function(e){
            var val=this.value;
            if(val=="") return;
            var source = field.attr("name");
            if(source==fids[0])
            {
                _rangeOptionReset(fids[1],otgo,"loading...");
                _rangeOptionReset(fids[2],otgo);
                _loadRangeOptions(fids[1],otgo,2,val);
            }else if(source==fids[1])
            {
                _rangeOptionReset(fids[2],otgo,"loading...");
                _loadRangeOptions(fids[2],otgo,3,val);
            }
        });
    });
    $(fids).each(function(i){
        if(i!=0) _rangeOptionReset(fids[i],otgo);
    });
    _rangeOptionReset(fids[0],otgo,"loading...");
    _loadRangeOptions(fids[0],otgo,1,"");
};
/**
 * 加载区域下拉项
 */
_loadRangeOptions=function(target,otgo,type,parent)
{
    $.getJSON(
        URL_FMT(SVR_CTX_PATH + "/system/sysDatAct!loadRange.do"),
        {"type":type,"parent":parent},
        function(data){
            if(data.status=="success")
            {
            	var tgo = $("select[name="+target+"]",otgo);
	            var list = tgo[0];
	            _rangeOptionReset(target,otgo);
	            $.each(data.values, function(i,item){
                    list.options[i+1]=new Option(item[1],item[0]);
	            });
	            var _dv = tgo.attr("defval");
	            if(_dv)
	            {
		            _optionSelected(tgo, _dv);
	            }
            }
	   }
    );
};
/**
 * 默认选中
 */
_optionSelected=function(tgo,dv)
{
    if(tgo && dv)
    {
        var list = tgo[0];
        var i = 0;
        for (i=0;i<list.options.length;i++)   
		{
			if(list.options[i].value == dv+'' || list.options[i].text == dv+'')   
			{
				list.options[i].selected = true;
                break;
            }
		}
        tgo.change();
    }
};
/**
 * 重置下拉项
 */
_rangeOptionReset=function(target,otgo,titlelabel)
{
	var obj = $("select[name='"+target+"']",otgo);
    if(!IS_PGOBJ(obj,true)) return;
    obj.empty();
    obj[0][0]=new Option(titlelabel?titlelabel:"请选择","");
};

/**
 * 格式化分页导航
 */
GBL_toNavPage=function(page,totalPage,pageSpan,jumpFuName)
{
	if(totalPage<2) return "";
	
	var pageFrom = parseInt((page - 1) / pageSpan) *pageSpan + 1;
	var pageEnd = pageFrom - 1 + pageSpan;
	if (pageEnd > totalPage)
		pageEnd = totalPage;
	
	var buff = "";
	var i = pageFrom;
	if (page > 1)
		buff += ("<a href=\"javascript:void(0)\" onclick=\""+jumpFuName+"(" + (page - 1) + ")\" title=\"第" + (page - 1) + "页\" class=\"pageon\">上一页</a>");
	else
		buff += ("<span class=\"pageon nopage\">上一页</span>");
	if (i > pageSpan)
		buff += ("<a href=\"javascript:void(0)\" onclick=\""+jumpFuName+"(" + (i - 1) + ")\" title=\"前几页\">...</a>");
	for (; i <= pageEnd; i++)
	{
		if (page == i)
			buff += ("<strong>" + i + "</strong>");
		else
			buff += ("<a href=\"javascript:void(0)\" onclick=\""+jumpFuName+"(" + i + ")\" title=\"第" + i + "页\">" + i + "</a>");
	}
	if (i < totalPage)
		buff += ("<a href=\"javascript:void(0)\" onclick=\""+jumpFuName+"(" + i + ")\" title=\"下几页\">...</a>");
	if (page < totalPage)
		buff += ("<a href=\"javascript:void(0)\" onclick=\""+jumpFuName+"(" + (page + 1) + ")\" title=\"第" + (page + 1) + "页\">下一页</a>");
	else
		buff += ("<span class=\"nopage\">下一页</span>");
	return buff;
};
/**
 * 过滤后用于title显示
 */
GBL_filter=function(str)
{
	str = str.replace(/<\/?[^>]*>/g,'');
	str = str.replace(/(\"|\'|\n)+/g,'“');
	return str;
};
/**
 * 过滤后用于title显示
 */
GBL_title_tip=function(val,rdat,ridx,nb)
{
	return !val?"":nb?GBL_filter(val):"<div title=\""+GBL_filter(val)+"\">"+val+"</div>";
};
/**
 * 给文本注册提示动作
 */
function _regTxtFieldOverTip(el)
{
	$(".existTip .tipField").each(function(i,e){
		var el = $(e);
		el.focus(function(){$(".showText",el.parent()).hide();});
		el.blur(function(){if($.trim($(this).val())=='') $(".showText",el.parent()).show();});
		$(".showText",el.parent()).click(function(){el.focus();});
	});	
};